package com.itheima.entry.factory;

import com.itheima.entry.controller.ISuperController;
import com.itheima.entry.service.ISuperService;


import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.Properties;

public class SuperFactory {
    private static Properties prop = new Properties();

    static {

        try {
            InputStream reader = ClassLoader.getSystemResourceAsStream("factory.properties");
            prop.load(reader);
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static ISuperController getController() {
        ISuperController o = null;
        try {
            Class<?> clazz = Class.forName(prop.getProperty("ISuperController"));
            o = (ISuperController) clazz.getConstructor().newInstance();
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
            e.printStackTrace();
        }

        return o;
    }

    public static ISuperService getService() {
        ISuperService o = null;
        try {
            Class<?> clazz = Class.forName(prop.getProperty("ISuperService"));
            o = (ISuperService) clazz.getConstructor().newInstance();
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
            e.printStackTrace();
        }

        return o;
    }
}
